package com.hellowx.qydcdev.controller;

import com.github.pagehelper.PageInfo;
import com.hellowx.qydcdev.annotaion.RequiredToken;
import com.hellowx.qydcdev.entity.Goods;
import com.hellowx.qydcdev.entity.GoodsVO;
import com.hellowx.qydcdev.entity.State;
import com.hellowx.qydcdev.entity.User;
import com.hellowx.qydcdev.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created on 2022/1/13.
 *
 * @author Hello WX
 */
@Api(tags = "商品模块")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @ApiOperation("按条件分页查询商品信息")
    @GetMapping("/list")
    @RequiredToken
    public ResponseEntity<PageInfo<GoodsVO>> listGoodsVOByPage(Goods goods, int pageNum, int pageSize,HttpServletRequest request){
        PageInfo<GoodsVO> goodsPageInfo = goodsService.selectGoodsBySearch(goods,pageNum,pageSize,request);
        return ResponseEntity.ok(goodsPageInfo);
    }

    @ApiOperation("通过商品id获取商品信息")
    @GetMapping("byId")
    @RequiredToken
    public ResponseEntity<GoodsVO> getGoodsById(Long goodsId){
        GoodsVO goods = goodsService.getGoodsById(goodsId);
        return ResponseEntity.ok(goods);
    }

    @ApiOperation("通过商品id删除商品信息")
    @DeleteMapping("/byId")
    @RequiredToken
    public ResponseEntity<String> deleteGoodsById(@RequestParam("goodsId") Long goodsId){
        String msg = goodsService.deleteGoodsById(goodsId);
        return ResponseEntity.ok(msg);
    }

    @ApiOperation("新增商品信息")
    @PostMapping("/add")
    @RequiredToken
    public ResponseEntity<String> addGoods(@RequestBody Goods goods, HttpServletRequest request) {
        User loginUser = (User) request.getAttribute("loginUser");
        goods.setBusinessId(loginUser.getBusinessId());
        String goodsId = goodsService.insertGoods(goods);
        return ResponseEntity.ok(goodsId);
    }

    @ApiOperation("查询商品口味,通过商品id")
    @GetMapping("/stateByGoodsId")
    @RequiredToken
    public ResponseEntity<List<State>> listStateListByGoodsId(Long goodsId){
        List<State> stateList = goodsService.listStateListByGoodsId(goodsId);
        return ResponseEntity.ok(stateList);
    }

    @ApiOperation("新增商品口味")
    @PostMapping("/addState")
    @RequiredToken
    public ResponseEntity<String> addState(@RequestBody State state){
        int row = goodsService.addState(state);
        String msg = row>0? "添加成功" : "新增失败";
        return ResponseEntity.ok(msg);
    }

    @ApiOperation("删除商品口味")
    @DeleteMapping("/deleteState")
    @RequiredToken
    public ResponseEntity<String> deleteState(Long id){
        int row = goodsService.deleteState(id);
        String msg = row>0? "删除成功" : "删除失败";
        return ResponseEntity.ok(msg);
    }

    @ApiOperation("更新商品信息,通过商品id")
    @PostMapping("/updateById")
    @RequiredToken
    public ResponseEntity<String> updateGoodsById(@RequestBody Goods goods, HttpServletRequest request) {
        String msg = goodsService.updateGoodsById(goods);
        return ResponseEntity.ok(msg);
    }

}
